package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysCommoditySetting;
import com.ruoyi.system.service.ISysCommoditySettingService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单设置Controller
 *
 * @author ruoyi
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/setting")
public class SysSettingController extends BaseController {
    @Autowired
    private ISysCommoditySettingService SettingService;

    private final Integer STATUS_ERROR = -1;
    private final Integer STATUS_FOLL = -2;

    /**
     * 查询订单设置列表
     */
    @RequiresPermissions("system:setting:list")
    @GetMapping("/list")
    public TableDataInfo list(SysCommoditySetting sysCommoditySetting) {
        startPage();
        List<SysCommoditySetting> list = SettingService.selectSettingList(sysCommoditySetting);
        return getDataTable(list);
    }

    /**
     * 导出订单设置列表
     */
    @RequiresPermissions("system:setting:export")
    @Log(title = "订单设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCommoditySetting sysCommoditySetting) {
        List<SysCommoditySetting> list = SettingService.selectSettingList(sysCommoditySetting);
        ExcelUtil<SysCommoditySetting> util = new ExcelUtil<SysCommoditySetting>(SysCommoditySetting.class);
        util.exportExcel(response, list, "订单设置数据");
    }

    /**
     * 获取订单设置详细信息
     */
    @RequiresPermissions("system:setting:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(SettingService.selectSettingById(id));
    }

    /**
     * 新增订单设置
     */
    @RequiresPermissions("system:setting:add")
    @Log(title = "订单设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCommoditySetting sysCommoditySetting) {
        int rows = SettingService.insertSetting(sysCommoditySetting);
        if (rows == STATUS_ERROR) {
            return AjaxResult.error("最多启用一个设置项");
        }
        return toAjax(rows);
    }

    /**
     * 修改订单设置
     */
    @RequiresPermissions("system:setting:edit")
    @Log(title = "订单设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCommoditySetting sysCommoditySetting) {
        int rows = SettingService.updateSetting(sysCommoditySetting);
        if (rows == STATUS_ERROR) {
            return AjaxResult.error("最多启用一个设置项");
        }
        return toAjax(rows);
    }

    /**
     * 删除订单设置
     */
    @RequiresPermissions("system:setting:remove")
    @Log(title = "订单设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult removes(@PathVariable Long[] ids) {
        int rows = SettingService.deleteSettingByIds(ids);
        if (rows == STATUS_ERROR) {
            return AjaxResult.error("必须保留一个设置项");
        }
        if (rows == STATUS_FOLL) {
            return AjaxResult.error("启用项不可删除");
        }
        return toAjax(rows);
    }

    /**
     * 删除订单设置
     */
    @RequiresPermissions("system:setting:remove")
    @Log(title = "订单设置", businessType = BusinessType.DELETE)
    @DeleteMapping("del/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        int rows = SettingService.deleteSettingById(id);
        if (rows == STATUS_FOLL) {
            return AjaxResult.error("启用项不可删除");
        }

        return toAjax(rows);
    }

    /**
     * 启用/停用修改
     */
    @Log(title = "订单设置", businessType = BusinessType.UPDATE)
    @RequestMapping("/changeSettingStatus")
    public AjaxResult changeSettingStatus(Long id, Long status) {
        return toAjax(SettingService.changeSettingStatus(id, status));
    }
}
